package com.hnkywl.sts.service.zjgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.zjgl.JgzsDao;
import com.hnkywl.sts.entity.cwgl.kmys.Cyfyys;
import com.hnkywl.sts.entity.zjgl.Jgzs;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by HL on 2015/9/18.
 */
@Service
public class JgzsService {

    private JgzsDao jgzsDao;

    @Transactional
    public String findAllJgzs(int pageNo, int pageSize) {
        Page<Jgzs> jgzsPage = this.jgzsDao.findAllJgzs(pageNo, pageSize);
        ;

        List<Jgzs> jgzsList = jgzsPage.getResult() == null ? new ArrayList<Jgzs>() : jgzsPage.getResult();
        String jgzsJson = BaseUtils.fetchCollectionToJson(jgzsList, "id", "jzz", "pjzs", "jgrq");
        return "{\"total\":" + jgzsPage.getTotalCount() + ",\"rows\":[" + jgzsJson + "]}";
    }

 /*   @Transactional
    public String findAllJgzs( int pageNo, int pageSize) {
        Page<Jgzs> jgzsPage = this.jgzsDao.findAllJgzs( pageNo, pageSize);
        List<Jgzs> jgzsList = jgzsPage.getResult() == null ? new ArrayList<Jgzs>() :jgzsPage.getResult();
        String jgzsJson = BaseUtils.fetchCollectionToJson(jgzsList, "id", "jzz", "pjzs", "jgrq");
        return "{\"total\":" + jgzsPage.getTotalCount() + ",\"rows\":[" + jgzsJson + "]}";
    }*/

    @Transactional
    public Jgzs findJgzsDetail(Long id) {
        return this.jgzsDao.get(id);
    }


    @Transactional
    public void saveJgzs(Jgzs jgzs) {


       /* Jgzs x = jgzsDao.findJgzsrq();
        if(jgzs.getJgrq()==x.getJgrq()){
            throw new RuntimeException("价格日期不能重复！");
        }*/

        if (jgzs.getJgrq() == null) {
            throw new RuntimeException("价格日期不能为空！");
        } else if (jgzs.getPjzs() == null) {
            throw new RuntimeException("平均指数不能为空！");
        } else if (jgzs.getJzz() == null) {
            throw new RuntimeException("基准值不能为空！");
        }
        Jgzs j = jgzsDao.findJgzsByJgrq(jgzs.getJgrq());
        j.setJgrq(jgzs.getJgrq());
        j.setPjzs(jgzs.getPjzs());
        j.setJzz(jgzs.getJzz());
        j.setDateCreated(new Date());
        j.setLastUpdated(jgzs.getDateCreated());
        j.setCreateUser(YgService.getCurrentYg());
        j.setCreateOrgan(YgService.getCurrentYg().getZggs());
        j.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        this.jgzsDao.save(j);
    }

    @Transactional
    public void deletejgzs(Long id) {
        Jgzs jgzs = jgzsDao.get(id);
        jgzs.setLastUpdated(new Date());
        jgzs.setDeleted(1);
        this.jgzsDao.save(jgzs);
    }

    @Transactional
    public void updatejgzs(Jgzs jgzs) {
        Jgzs clone = this.findJgzsDetail(jgzs.getId());
        clone = (Jgzs) ReflectionUtils.getInstance(clone, jgzs);
        clone.setLastUpdated(new Date());
        this.jgzsDao.save(clone);
    }


    @Transactional
    public void save(Jgzs jgzs) {
        Jgzs c = jgzsDao.get(jgzs.getId());
        c.setJgrq(jgzs.getJgrq());
        c.setJzz(jgzs.getJzz());
        c.setPjzs(jgzs.getPjzs());
        jgzsDao.save(c);
    }


  /*  @Transactional
    public void saveJgzs(Jgzs jgzs) {
        this.jgzsDao.save(jgzs);
    }

*/

    public boolean ajaxValidate(Long id, String value, Object field) {
        return jgzsDao.checkUnique(id, value, field);
    }

    @Autowired
    public void setJgzsDao(JgzsDao jgzsDao) {
        this.jgzsDao = jgzsDao;
    }
}
